package com.letv.android.client.utils;

import android.content.Context;
import android.os.Handler;
import com.letv.android.young.client.R;
import com.letv.core.constant.PlayConstant;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.NetworkUtils;
import com.letv.datastatistics.util.PageIdConstant;
import com.ledim.utils.ToastUtil;
import java.util.regex.PatternSyntaxException;

//import com.letv.lesophoneclient.ex.LesoOnResumCallBack;
//import com.letv.lesophoneclient.ex.LesoParam;
//import com.letv.plugin.pluginconfig.commom.JarConstant;
//import com.letv.plugin.pluginloader.loader.JarLoader;


/**
 * 页面跳转工具类
 */
public class GotoPageUtils {
    private Handler mHandler;
    private static String TAG = "GotoPageUtils";
    /**
     * intent页面跳转fragment tag参数key
     */
    public static String GOTO_PAGE_TAG_KAY = "ledimVedioTagBean";

    /**
     * intent 直播子页id
     */
    public static String GOTO_CHILD_LIVE_PAGE_KAY = "childLiveId";
    /**
     * intent 频道id
     */
    public static String GOTO_CHANNEL_CID_KEY = "cid";
    /**
     * intent 频道pageid
     */
    public static String GOTO_CHANNEL_PAGE_ID_KEY = "pageid";

    /**
     * 禁止直接构造
     */
    private GotoPageUtils() {
    }

    public static GotoPageUtils getSingleton() {
        return SingletonHolder.mGotoPageUtils;
    }

    private static class SingletonHolder {
        static GotoPageUtils mGotoPageUtils = new GotoPageUtils();
    }

    /**
     * 从网页跳转
     *
     * @return 跳转是否成功
     */
//    public boolean gotoPageFromWebview(Context context, String url) {
//        if (TextUtils.isEmpty(url)) {
//            return false;
//        }
    //        LogInfo.log(LedimVedioTagBean + "||wlx", "webview 跳转客户端 url=" + url);
//        if (mHandler == null) {
//            mHandler = new Handler(context.getMainLooper());
//        }
//        if ("Home".equals(url)) {
//            return jumpToHome(context);
//        } else if (url.startsWith("1711_")) {//1711是频道页板块的id，CMS后台定义的，无特殊情况不会更改
//            return jumpToChannel(context, url);
//        } else if (url.startsWith("Me_")) {//我的
//            return jumpToMe(context, url);
//        } else if (url.startsWith("Live_")) {//直播
//            return jumpToLive(context, url);
//        } else if (url.startsWith("Hot")) {//热点
//            return jumpToHot(context);
//        } else if (url.startsWith("Topics")) {//专题
//            return jumpToTopics(context);
//        } else if (url.startsWith("Search")) {//搜索
////            return jumpToSearch(context);
//        } else if (url.startsWith("NiceApp")) {//应用推荐
//            return jumpToNiceApp(context);
//        } else {
//            ToastUtils.showToast(context, "没有此跳转指令！");
//            return false;
//        }
//    }

    /**
     * 应用推荐
     *
     * @param context
     * @return
     */
    private boolean jumpToNiceApp(final Context context) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
//                Intent i = new Intent(context, RecommendFragmentActivity.class);
//                context.startActivity(i);
            }
        });
        return true;
    }

    /**
     * 跳到搜索
     *
     * @param context
     * @return
     */
//    private boolean jumpToSearch(final Context context) {
//        mHandler.post(new Runnable() {
//            @Override
//            public void run() {
//                LogInfo.log("wlx", "跳转搜索");
//                Class clazz_lesoMain = JarLoader.loadClass(context, JarConstant.LETV_LESO_NAME, JarConstant.LETV_LESO_PACKAGENAME,
//                        "ui.LeSoMainActivity");
//                JarLoader.invokeStaticMethod(clazz_lesoMain, "launch", new Class[] { Context.class, LesoOnResumCallBack.class,
//                        LesoParam.class }, new Object[] { context, null, null });
//
//            }
//        });
//        return true;
//    }

    private boolean jumpToTopics(final Context context) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
//                Intent intent = new Intent(context, LetvTopicActivity.class);
//                context.startActivity(intent);
            }
        });
        return true;
    }

    private boolean jumpToHot(final Context context) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
//                Intent intent = new Intent(context, LetvHotActivity.class);
//                context.startActivity(intent);
            }
        });
        return true;
    }

    /**
     * 跳转到直播
     *
     * @param context
     * @param url
     * @return
     */
    private boolean jumpToLive(final Context context, String url) {
        try {
            String[] strings = url.split("_");
            if (strings.length == 2) {
                final String ct = strings[1];
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
//                        Intent intent = new Intent(context, MainActivity.class);
//                        intent.putExtra(GOTO_PAGE_TAG_KAY, FragmentConstant.TAG_FRAGMENT_LIVE);
//                        intent.putExtra(GOTO_CHILD_LIVE_PAGE_KAY, getLiveChildId(ct));
//                        context.startActivity(intent);
                    }
                });
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取直播子页id
     *
     * @param pageId 网页传过来的直播页id
     * @return 转换后的直播子页
     */
    public String getLiveChildId(String pageId) {
        LogInfo.log("live_", "pageId = " + pageId);
        if (pageId.equals(PageIdConstant.onLiveremenCtegoryPage)
                || pageId.equals(PlayConstant.CHANNEL_TYPE_VALUE_REMEN)) {
            return PlayConstant.CHANNEL_TYPE_VALUE_REMEN;
        } else if (pageId.equals(PageIdConstant.onLiveLunboCtegoryPage)
                || pageId.equals(PlayConstant.CHANNEL_TYPE_VALUE_LUNBO)) {
            return PlayConstant.CHANNEL_TYPE_VALUE_LUNBO;
        } else if (pageId.equals(PageIdConstant.onLiveWeishiCtegoryPage)
                || pageId.equals(PlayConstant.CHANNEL_TYPE_VALUE_WEISHI)) {
            return PlayConstant.CHANNEL_TYPE_VALUE_WEISHI;
        } else if (pageId.equals(PageIdConstant.onLiveSportCtegoryPage)
                || pageId.equals(PlayConstant.CHANNEL_TYPE_VALUE_SPORTS)) {
            return PlayConstant.CHANNEL_TYPE_VALUE_SPORTS;
        } else if (pageId.equals(PageIdConstant.onLiveMusicCtegoryPage)
                || pageId.equals(PlayConstant.CHANNEL_TYPE_VALUE_MUSIC)) {
            return PlayConstant.CHANNEL_TYPE_VALUE_MUSIC;
        } else if (pageId.equals(PageIdConstant.onLiveEntertainmentCtegoryPage)
                || pageId.equals(PlayConstant.CHANNEL_TYPE_VALUE_ENT)) {
            return PlayConstant.CHANNEL_TYPE_VALUE_ENT;
        }
        return null;
    }

    /**
     * 跳转“我的”页面的子页面需要注意有些子页面需要区分是否是登录状态，有的需要获取用户信息
     * 页面会因为是否登录而现实不用的界面，比如如果用户没登录，在播放记录页面不会有信息；否者有记录
     *
     * @param context
     * @param url
     * @return
     */
    private boolean jumpToMe(final Context context, String url) {
        if ("Me_Record".equals(url)) {
            mHandler.post(new Runnable() {
                              @Override
                              public void run() {
//                                  MyPlayRecordActivity.launch(context);
                              }
                          }
            );
            return true;
        } else if ("Me_Favorites".equals(url)) {
            mHandler.post(new Runnable() {
                              @Override
                              public void run() {
//                                  MyCollectActivity.launch(context);
                              }
                          }
            );
            return true;
        } else if ("Me_Download".equals(url)) {
            mHandler.post(new Runnable() {
                              @Override
                              public void run() {
//                                  MyDownloadActivity.launch(context, 0);//可能需要选择跳到下载页面中的哪个子页面
                              }
                          }
            );
            return true;
        } else if ("Me_Checkout".equals(url)) {
            mHandler.post(new Runnable() {
                              @Override
                              public void run() {
//                                  VipProductsActivity.launch(context, context.getResources().getString(R.string.pim_vip_good_title));
                              }
                          }
            );
            return true;
        } else if ("Me_Points".equals(url)) {
            if (NetworkUtils.isNetworkAvailable(context)) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
//                        LetvWebViewActivity.launch(context, LetvTools.getTextFromServer("90043", LoginConstant.LETV_MALL_JIFEN_WEBSITE), null);
                    }
                });
                return true;
            } else {
                ToastUtil.toast(R.string.net_error);
                return false;
            }
        } else if ("Me_Login".equals(url)) {
//            mHandler.post(new Runnable() {
//                @Override
//                public void run() {
//                    SettingsMainActivity.launch(context);
//                }
//            });
            return true;
        }
        else if ("Me_Setting".equals(url)) {
            mHandler.post(new Runnable() {
                              @Override
                              public void run() {
//                                  SettingsMainActivity.launch(context);
                              }
                          }
            );
            return true;
        }
        return false;
    }

    /**
     * 跳转到频道页
     * 格式： 1711_1_1002697474 或者 1711_1001(杜比，没有pageId)
     * 频道列表数据：http://test2.m.letv.com/android/mod/mob/ctl/channelinfopageid/act/index/pcode/010110000/version/5.5.mindex.html
     * @param context
     * @param url
     * @return
     */
    private boolean jumpToChannel(final Context context, String url) {
        if (!NetworkUtils.isNetworkAvailable(context)) {
            ToastUtil.toast(R.string.load_data_no_net);
            return false;
        }
        String[] strings = url.split("_");
        String cid = "";
        String pageid = "";
        if (strings.length == 3) {
            cid = strings[1];
            pageid = strings[2];
        } else if (strings.length == 2) {
            cid = strings[1];
        }
        LogInfo.log(TAG + "||wlx", "strings: " + strings + ", cid: " + cid + ", pageid: " + pageid);

        final String finalCid = cid;
        final String finalPageid = pageid;
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
//                    if (LetvApplication.getInstance().getChannelList() == null) {
                    //                        LogInfo.log(LedimVedioTagBean + "||wlx", "channelHashMap 没有数据");
//                        getChannelList(context, finalCid, finalPageid);
//                    } else {
//                        gotoChannel(context, finalCid, finalPageid);
//                    }
                } catch (PatternSyntaxException e) {
                    LogInfo.log(TAG + "||wlx", "跳转频道异常");
                    e.printStackTrace();
                }
            }
        });
        return false;
    }

    /**
     * 跳到频道页
     * @param context
     * @param cid
     * @param pageId
     */
    private void gotoChannel(Context context, String cid, String pageId) {
//        Intent intent = new Intent(context, MainActivity.class);
//        intent.putExtra(GOTO_PAGE_TAG_KAY, FragmentConstant.TAG_FRAGMENT_CHANNEL);
//        intent.putExtra(GOTO_CHANNEL_CID_KEY, cid);
//        intent.putExtra(GOTO_CHANNEL_PAGE_ID_KEY, pageId);
//        context.startActivity(intent);
    }

    /**
     * 获取频道信息
     *
     * @param context
     * @param cid
     * @param pageId
     */
//    private void getChannelList(final Context context, final String cid, final String pageId) {
//        final ChannelsParser parser = new ChannelsParser();
//        new LetvRequest<ChannelListBean>(context, ChannelListBean.class)
//                .setUrl(MediaAssetApi.getInstance().getChannelListUrl(null))
//                .setParser(parser)
//                .setCallback(new SimpleResponse<ChannelListBean>() {
//                    @Override
//                    public void onNetworkResponse(VolleyRequest<ChannelListBean> request, ChannelListBean result,
//                                                  DataHull hull, VolleyResponse.NetworkResponseState state) {
//                        LogInfo.log("jc666", "http:" + state);
//                        if (state == VolleyResponse.NetworkResponseState.SUCCESS) {
//                            gotoChannel(context, cid, pageId);
//                        }
//                    }
//                }).add();
//    }

    /**
     * 跳到首页
     *
     * @param context
     * @return
     */
    private boolean jumpToHome(final Context context) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
//                Intent intent = new Intent(context, MainActivity.class);
//                intent.putExtra(GOTO_PAGE_TAG_KAY, FragmentConstant.TAG_FRAGMENT_HOME);
//                context.startActivity(intent);
            }
        });
        return true;
    }
}
